package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.ClueStatusConstants;
import com.qk.domain.clue.ClueDO;
import com.qk.dto.clue.ClueDTO;
import com.qk.dto.clue.ClueListDTO;
import com.qk.dto.clue.GetCluePoolDTO;
import com.qk.dto.clue.UpdateClueInfoDTO;
import com.qk.entity.Clue;
import com.qk.management.aop.LogAnno;
import com.qk.management.service.ClueService;
import com.qk.vo.clue.ClueVO;
import com.qk.domain.clue.FalsyClueDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author Just、倾城
 * @created 2025-10-13
 * @description 线索管理的控制器
 *
 */
@Slf4j
@RestController
@RequestMapping("/clues")
public class ClueController {
@Autowired
private ClueService clueService;


@GetMapping("/pool")
public Result list(GetCluePoolDTO dto){
    log.info("获取线索池");
    PageResult<FalsyClueDO> falsyClueDOS = clueService.getCluePool(dto);
    return Result.success(falsyClueDOS);
}
@LogAnno
@PutMapping("/toBusiness/{id}")
public Result convertToBussiness(@PathVariable Integer id){
    log.info("转换线索为商机,id:{}",id);
    clueService.convertToBussiness(id);
    return Result.success();
}
@LogAnno
@PutMapping
public Result updateClueInfoById(@RequestBody UpdateClueInfoDTO dto){
    clueService.updateClueInfoById(dto);
    return Result.success();
}
@GetMapping("/{id}")
public Result getClueInfoById(@PathVariable Integer id){
   ClueVO vo =  clueService.getClueInfoById(id);
   return Result.success(vo);
}
@LogAnno
@PutMapping("/assign/{clueId}/{userId}")
public Result updateClueById(@PathVariable Integer clueId,@PathVariable Integer userId){
    clueService.updateById(Clue.builder()
            .id(clueId)
            .userId(userId)
            .updateTime(LocalDateTime.now())
            .status(ClueStatusConstants.WAIT_FOLLOW_UP)
            .build());
    return Result.success();
}

@GetMapping
public Result list(ClueListDTO dto){
    PageResult<ClueDO> result = clueService.clueList(dto);
    return Result.success(result);
}
@LogAnno
@PostMapping
    public Result addClue(@RequestBody ClueDTO clueDTO){
        log.info("新增线索");
        clueService.addClue(clueDTO);
        return Result.success();
    }
}
